package com.gosophia.conferenceManagement.common.ws.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.gosophia.commons.DozerChangeUtil;
import com.gosophia.commons.OrderCondition;
import com.gosophia.commons.Page;
import com.gosophia.commons.PropertyFilter;
import com.gosophia.commons.PropertyFilter.OperatorType;
import com.gosophia.conferenceManagement.common.ws.ConferenceHandlerListWebService;
import com.gosophia.dataDictionary.dto.DataValueDTO;
import com.gosophia.taskManagement.dto.JobDTO;
import com.gosophia.taskManagement.dto.MissionDTO;
import com.gosophia.taskManagement.entity.Job;
import com.gosophia.taskManagement.service.TaskManagementService;
import com.gosophia.taskManagement.ws.TaskManagementWebService;
import com.gosophia.userManagement.dto.UserInfoDTO;

@Service("ConferenceHandlerListWebService")
@Transactional
public class ConferenceHandlerListWebServiceImpl implements
		ConferenceHandlerListWebService {

	/** 会议任务类型Id */
	@SuppressWarnings("unused")
	private static Long CONFERENCEMISSIONTYPEID = 1009L;

	/** 会议置办列表显示下列JobType的Job */
	public static String[] conferenceHandlerListJobTypes = { "审批会议申请",
			"取回会议申请", "修改会议申请", "发起会议", "方案起草", "方案审批", "方案审批取回", "方案修改",
			"方案定稿","会前准备", "预通知起草", "预通知审批", "预通知审批取回", "预通知修改", "预通知定稿", "预通知发送",
			"正式通知起草", "正式通知审批", "正式通知审批取回", "正式通知修改", "正式通知定稿", "正式通知发送",
			"会务完成确认", "会务准备", "总结起草", "总结审批", "总结审批取回", "总结修改", "总结定稿", "总结发送" };

	@Autowired
	private TaskManagementService taskManagementService;

	@Autowired
	private TaskManagementWebService taskManagementWebService;

	public Page<JobDTO> findConferenceHandleListByUserInfoId(Long userInfoId,
			int pageNo, int pageSize, List<PropertyFilter> propertyFilters,
			List<OrderCondition> orderConditions, String jobType) {
		Assert.isTrue(pageNo > 0, "页码必须大于零");
		Assert.isTrue(pageSize > 0, "每页最大记录数必须大于零");
		Assert.notNull(userInfoId, "用户基本信息Id不能为null");
		Page<Job> page = new Page<Job>();
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		if (jobType == null) {
			page = taskManagementService.findMyTodoJobByJobTypeAndByPage(
					userInfoId, CONFERENCEMISSIONTYPEID, page, propertyFilters,
					orderConditions, conferenceHandlerListJobTypes);
		} else {
			page = taskManagementService.findMyTodoJobByJobTypeAndByPage(
					userInfoId, CONFERENCEMISSIONTYPEID, page, propertyFilters,
					orderConditions, jobType);
		}

		List<Job> jobs = page.getResult();
		List<JobDTO> jobDTOs = new ArrayList<JobDTO>();
		if (jobs != null && jobs.size() != 0) {
			for (Job job : jobs) {
				JobDTO jobDTO = DozerChangeUtil.changeBeforeToAfter(job,
						JobDTO.class);
				MissionDTO mission = DozerChangeUtil.changeBeforeToAfter(job
						.getMission(), MissionDTO.class);
				UserInfoDTO userInfo = DozerChangeUtil.changeBeforeToAfter(job
						.getMission().getCreator(), UserInfoDTO.class);
				mission.setCreatorDTO(userInfo);
				DataValueDTO importanceDTO = DozerChangeUtil
						.changeBeforeToAfter(job.getMission().getImportance(),
								DataValueDTO.class);
				mission.setImportanceDTO(importanceDTO);
				jobDTO.setMissionDTO(mission);
				UserInfoDTO source = DozerChangeUtil.changeBeforeToAfter(job
						.getSource(), UserInfoDTO.class);
				jobDTO.setSourceDTO(source);
				taskManagementWebService.changePromptJob(jobDTO);
				jobDTOs.add(jobDTO);
			}
		}
		Page<JobDTO> pageJob = new Page<JobDTO>();
		pageJob.setPageNo(page.getPageNo());
		pageJob.setPageSize(page.getPageSize());
		pageJob.setResult(jobDTOs);
		pageJob.setTotalCount(page.getTotalCount());

		// Page<JobDTO> pageJob = new Page<JobDTO>();
		// pageJob.setPageNo(pageNo);
		// pageJob.setPageSize(pageSize);
		// pageJob.setResult(getTestJobDTOs());
		// pageJob.setTotalCount(1);

		return pageJob;
	}

	public Page<JobDTO> findConferenceHandleListByUserInfoId(Long userInfoId,
			int pageNo, int pageSize, String condition,
			List<OrderCondition> orderConditions) {
		Assert.isTrue(pageNo > 0, "页码必须大于零");
		Assert.isTrue(pageSize > 0, "每页最大记录数必须大于零");
		Assert.notNull(userInfoId, "用户基本信息Id不能为null");
		List<PropertyFilter> propertyFilters = new ArrayList<PropertyFilter>();
		if (condition != null && !condition.equals("")) {
			PropertyFilter propertyFilter1 = new PropertyFilter(
					"mission.missionCode", OperatorType.LIKE, condition);
			PropertyFilter propertyFilter2 = new PropertyFilter(
					"mission.missionName", OperatorType.LIKE, condition);
			// PropertyFilter propertyFilter3 = new PropertyFilter(
			// "mission.creator.userName", OperatorType.LIKE,
			// condition);
			//会议置办要求用'来自'模糊查询,而不是创建人creator
			PropertyFilter propertyFilter3 = new PropertyFilter(
					"source.userName", OperatorType.LIKE, condition);
			List<PropertyFilter> propertyFilters2 = new ArrayList<PropertyFilter>();
			propertyFilters2.add(propertyFilter2);
			List<PropertyFilter> propertyFilters3 = new ArrayList<PropertyFilter>();
			propertyFilters3.add(propertyFilter3);
			propertyFilter2.setOrFilters(propertyFilters3);
			propertyFilter1.setOrFilters(propertyFilters2);
			propertyFilters.add(propertyFilter1);
		}

		Page<Job> page = new Page<Job>();
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		page = taskManagementService.findMyTodoJobByJobTypeAndByPage(// 暂时用公文的刷列表，以后等会议的流程完成后在改过来
				userInfoId, CONFERENCEMISSIONTYPEID, page, propertyFilters,// TODO
				orderConditions, conferenceHandlerListJobTypes);
		// page = taskManagementService.findMyTodoJobByJobTypeAndByPage(
		// userInfoId, 1008L, page, propertyFilters,orderConditions,
		// "approve","getBack", "modify", "documentFinalize");
		List<Job> jobs = page.getResult();
		List<JobDTO> jobDTOs = new ArrayList<JobDTO>();
		if (jobs != null && jobs.size() != 0) {
			for (Job job : jobs) {
				JobDTO jobDTO = DozerChangeUtil.changeBeforeToAfter(job,
						JobDTO.class);
				MissionDTO mission = DozerChangeUtil.changeBeforeToAfter(job
						.getMission(), MissionDTO.class);
				UserInfoDTO userInfo = DozerChangeUtil.changeBeforeToAfter(job
						.getMission().getCreator(), UserInfoDTO.class);
				mission.setCreatorDTO(userInfo);
				DataValueDTO importanceDTO = DozerChangeUtil
						.changeBeforeToAfter(job.getMission().getImportance(),
								DataValueDTO.class);
				mission.setImportanceDTO(importanceDTO);
				jobDTO.setMissionDTO(mission);
				UserInfoDTO source = DozerChangeUtil.changeBeforeToAfter(job
						.getSource(), UserInfoDTO.class);
				jobDTO.setSourceDTO(source);
				taskManagementWebService.changePromptJob(jobDTO);
				jobDTOs.add(jobDTO);
			}
		}
		Page<JobDTO> pageJob = new Page<JobDTO>();
		pageJob.setPageNo(page.getPageNo());
		pageJob.setPageSize(page.getPageSize());
		pageJob.setResult(jobDTOs);
		pageJob.setTotalCount(page.getTotalCount());

		// Page<JobDTO> pageJob = new Page<JobDTO>();
		// pageJob.setPageNo(pageNo);
		// pageJob.setPageSize(pageSize);
		// pageJob.setResult(getTestJobDTOs());
		// pageJob.setTotalCount(1);

		return pageJob;
	}

	// private List<JobDTO> getTestJobDTOs(){
	// MissionDTO mission1 = new MissionDTO();
	// mission1.setMissionCode("hygl20110323001");
	// mission1.setMissionName("会议管理-娜娜测试页面001");
	// UserInfoDTO user1 = new UserInfoDTO();
	// user1.setUserName("王娜娜");
	// mission1.setCreatorDTO(user1);
	// mission1.setScheduleEndDate(new Date());
	// DataValueDTO importanceDTO = new DataValueDTO();
	// importanceDTO.setValue("领导交办");
	// mission1.setImportanceDTO(importanceDTO);
	// JobDTO job1 = new JobDTO();
	// job1.setMissionDTO(mission1);
	// job1.setJobType("方案起草");
	// job1.setAlertFlag("1");
	// List<JobDTO> jobs = new ArrayList<JobDTO>();
	// System.out.println("获取一个job");
	// jobs.add(job1);
	// return jobs;
	// }

}
